<?php

declare(strict_types=1);

namespace App\Admin\Controller\Setting;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Middleware\PermissionMiddleware;
use App\Admin\Request\Setting\SettingConfigCreateRequest;
use App\Admin\Service\Setting\SettingConfigService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\Middleware;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\NewbieController;
use support\Request;
use support\Response;
use Webman\Route;

/**
 * 系统配置控制器
 * Class SystemConfigController
 * @package App\Setting\Controller\Settings
 */
#[Controller(prefix: "setting/config")]
class SystemConfigController extends NewbieController {
    #[Inject]
    protected SettingConfigService $service;

    /**
     * 获取系统组配置 （不验证身份和权限）
     * @return Response
     */
    #[GetMapping("getSysConfig")]
    public function getSysConfig (): Response {
        return $this->success($this->service->getSystemGroupConfig());
    }

    /**
     * 获取系统组配置
     * @return Response
     */
    #[GetMapping("getSystemConfig"), Auth, Permission("setting:config:getSystemConfig")]
    public function getSystemGroupConfig (): Response {
        return $this->success($this->service->getSystemGroupConfig());
    }

    /**
     * 获取扩展组配置
     * @return Response
     */
    #[GetMapping("getExtendConfig"), Auth, Permission("setting:config:getExtendConfig")]
    public function getExtendGroupConfig (): Response {
        return $this->success($this->service->getExtendGroupConfig());
    }

    /**
     * 按组名获取配置
     * @param Request $request
     * @return Response
     */
    #[GetMapping("getConfigByGroup"), Auth]
    public function getConfigByGroup (Request $request): Response {
        return $this->success($this->service->getConfigByGroup($request->input('groupName', '')));
    }

    /**
     * 按key获取配置
     * @param Request $request
     * @return Response
     */
    #[PostMapping("getConfigByKey")]
    public function getConfigByKey (Request $request): Response {
        return $this->success($this->service->getConfigByKey($request->input('key', '')));
    }

    /**
     * 保存系统配置组数据
     * @param Request $request
     * @return Response
     */
    #[PostMapping("saveSystemConfig"), Auth, Permission("setting:config:saveSystemConfig")]
    public function saveSystemConfig (Request $request): Response {
        return $this->service->saveSystemConfig($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 保存配置
     * @param SettingConfigCreateRequest $request
     * @return Response
     */
    #[PostMapping("save"), Auth, Permission("setting:config:save")]
    public function save (SettingConfigCreateRequest $request): Response {
        return $this->service->save($request->validated()) ? $this->success() : $this->error();
    }

    /**
     * 更新配置
     * @param SettingConfigCreateRequest $request
     * @return Response
     */
    #[PostMapping("update"), Auth, Permission("setting:config:update")]
    public function update (SettingConfigCreateRequest $request): Response {
        return $this->service->updated($request->validated()) ? $this->success() : $this->error();
    }

    /**
     * 删除配置
     * @param string $key
     * @return Response
     */
    #[DeleteMapping("delete/{key}"), Auth, Permission("setting:config:delete")]
    public function delete (string $key): Response {
        return $this->service->delete($key) ? $this->success() : $this->error();
    }

    /**
     * 清除配置缓存
     * @return Response
     */
    #[PostMapping("clearCache"), Auth, Permission("setting:config:clearCache")]
    public function clearCache (): Response {
        return $this->service->clearCache() ? $this->success() : $this->error();
    }
}